package twentytwenty;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2020-12-16 15:17
 * @Modified By:
 */
public class RepeatedSubstringPattern_459 {
    public static void main(String [] args){
        String s = "abaababaab";
        System.out.println(repeatedSubstringPattern(s));
    }

    public static boolean repeatedSubstringPattern(String s){
        StringBuilder stringBuilder = new StringBuilder();
        int len = s.length();
        int i = 1;
        do {
            if (len % i == 0) {
                if (i == len) {
                    return false;
                }
                String sub = s.substring(0, i);
                stringBuilder.append(sub.repeat(Math.max(0, len / i)));
                if (stringBuilder.toString().equals(s)) {
                    return true;
                }
                stringBuilder.delete(0, len);
            }
            i++;
        } while (len >= i);
        return false;
    }
}
